package com.lions.client.model;

/**
 * Call this list through Model.
 * This list holds all of the modules.
 * @author Brad
 *
 */
public class ModuleList extends java.util.LinkedList<Module> {

	/**
	 * Default Constructor
	 * Sets all of its variables to false
	 */
	public ModuleList() {
		super();
	}

	/**
	 * Copy constructor ModuleArray. Copies the elements in a new list. New list
	 * may not be sorted.
	 * 
	 * @moduleArray ModuleArray type list
	 */
	public ModuleList(ModuleList moduleArray) {
		for (Module e : moduleArray) {
			if (e != null)
				this.add(e);
		}
	}

	/**
	 * Copy constructor primitive array. Copies the elements in a new list. New
	 * list may not be sorted.
	 * 
	 * @array
	 */
	public ModuleList(Module[] primArray) {

		int offset = 0;
		for (int i = 0; i < primArray.length; i++) {
			if (primArray[i] != null) {
				this.add(primArray[i - offset]);
			} else {
				offset++;
			}
		}
	}
	
	public int getModuleIndex(Module m) {
		int index = -1;
		for (int i = 0; i < this.size(); i++) {
			if (m.equals(this.get(i))) {
				index = i;
			}
		}
		return index;
	}
	
	public Module getModuleAtCoord(int x, int y) {
		Module mod = null;
		for(Module m  : this) {
			if ((m.getX() == x) && (m.getY() == y)) {
				mod = m;
			}
		}
		return mod;
	}
	
	public boolean removeModuleAtCoord(int x, int y) {
		boolean removed = false;
		for(Module m  : this) {
			if ((m.getX() == x) && (m.getY() == y)) {
				removed = this.removeModule(m);
			}
		}
		return removed;
	}
	
	private boolean removeModule(Module m) {
		boolean removed = false;
		for (int i = 0; i < this.size(); i++) {
			if ((m.getX() ==  this.get(i).getX()) &&
					(m.getY() == this.get(i).getY())) {
				this.remove(i);
			}
		}
		return removed;
	}

}
